<?php
/*  remoteLoginFileReader
    @version 0.9.1
 
    Copyright (C) 2011  Vespo Marco | http://thelairofthemad.altervista.org/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

-----------------------------------------------------------------------------------------------*/
include_once 'remoteFileReader.class.php';

class remoteLoginFileReader extends remoteFileReader {

	protected $cc,$pw,$id,$log,$xml,$dataFormat,$secretWord;
	
	protected $loginURL,$cookiePath,$curlLogPath;

	function __construct($id,$pw,$dataURL,$loginURL,$secretWord,$dataFormat){				
		$this->cookiePath='cookie.txt';
		$this->curlLogPath='cur_log.txt';				
		$this->dataURL=$dataURL;
		$this->loginURL=$loginURL;		
		$this->secretWord=$secretWord;
		$this->dataFormat=$dataFormat;		
		$this->id=$id;
		$this->pw=$pw;		
		$this->login=true;
		$this->log=false;//curl log disable for default	
		libxml_use_internal_errors(true);	
	}
	
	public function getData(){
	
		if (function_exists('curl_init')) { 
			$this->cc = curl_init();
		
			if($this->log){
				error_reporting(E_ALL);
				libxml_use_internal_errors(true);
				$fp=fopen($this->curlLogPath,'a+');
				curl_setopt ($this->cc, CURLOPT_STDERR, $fp);
				curl_setopt ($this->cc, CURLOPT_VERBOSE, 1);
			}
		
			curl_setopt ($this->cc, CURLOPT_URL, $this->dataURL);
			curl_setopt ($this->cc, CURLOPT_POST, 0);
			curl_setopt ($this->cc, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt ($this->cc, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt ($this->cc, CURLOPT_USERAGENT, $this->getRandomUserAgent());
			curl_setopt ($this->cc, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt ($this->cc, CURLOPT_REFERER, $this->dataURL);	
			curl_setopt ($this->cc, CURLOPT_COOKIEFILE,$this->cookiePath);
			$this->data=curl_exec($this->cc);
			
			if($this->dataFormat)
				$this->loadXml();       
			if ((strpos($this->data,$this->secretWord)===0)||((!$this->xml)&&($this->dataFormat)))
				$this->login();
			else{
				curl_close($this->cc);
				if($this->log) fclose($fp);
			}
			return true;
		}else return false;	
	}

	protected function login(){
		curl_setopt ($this->cc, CURLOPT_URL, $this->loginURL);
		curl_setopt ($this->cc, CURLOPT_POST, 1);
		curl_setopt ($this->cc, CURLOPT_POSTFIELDS, 'loginName='.$this->id.'&password='.$this->pw);
		curl_setopt ($this->cc, CURLOPT_COOKIEJAR,$this->cookiePath);
		curl_exec($this->cc);
		$this->getData();
	}
	
	private function loadXml(){
		if(!isset($this->xml)||(empty($this->xml)))$this->xml=simplexml_load_string($this->data);
		return $this->xml; 
	}

	public function getLoginUrl(){return $this->loginURL;}
	
	public function getXml(){return $this->xml;}
		
	public function setLoginUrl($loginURL){$this->loginURL=$loginUrl;}
	
	public function setCookiePath($cookiePath){$this->cookiePath=$cookiePath;}
	
	public function setCurlLogPath($curlLogPath){$this->curlLogPath=$curlLogPath;}
	
	public function setLog($log){$this->log=$log;}
}
?>
